/* ==============================================================================
 * 创 建 者：LYT
 * 创建日期：2018-03-14	
 * 功能描述：

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/

//商品列表接口
var getGoodsListUrl = baseUrl + 'MallUF/GetGoodsList';
//推荐商品
var getRecommendGoodsUrl = baseUrl + 'ApiGoods/GetIsRecommendGoods';
//搜索商品
var searchGoodUrl = baseUrl + 'MallUF/GetGoodsList';
//获取商品分类
var getGoodsTypeUrl = baseUrl + 'MallUF/GetGoodsType';




var type = getURLParameter("type");//TJSP推荐商品  GWSC消费商城  BDSC宝豆商城     CZK购物卡  
var oid = getURLParameter("oid");//分类商品
var goodName = getURLParameter("goodName");//搜索商品


var mescroll;
var vu;

$(function() {
	mui.init();
	initVue();
	Vue.nextTick(function(){
		bindEvent();
	})
	
	
	
})

function initVue(){
	vu=new Vue({
		el:'#mescroll',
		data:{
			searchVal:'', //搜索内容
			
			GoodsList:[],
			chooseGoodInfo:[],
			pageIndex:1,
			PageSize:10,
			TotalPageCount:'',
			memberInfo : '',
			
			isShow : false,//是否显示分类选择框
			searchtype : '',//筛选种类oid
			sortNum : '',// 为空时 默认排序  1销量    2价格升序   3价格降序
			priceType:'asc',
			firstTypes : [],//1级分类   
			secondTypes : [],//2级分类   
			thirdTypes : [],//3级分类   
			chooseGoodsTypeOid : '',//当前选择的类别oid
		},
		mounted: function() {
			initMescroll();
			this.getGoodsType();
		},
		methods:{
			getGoodsList : getGoodsList,
			getGoodsType : getGoodsType,
			getGoodListByTypeOid : getGoodListByTypeOid,
			search:function(){
				if(this.searchVal!=''){
					location.href='searchList.html?goodName='+this.searchVal;
				}else{
					mui.toast('请输入搜索关键词');
				}
			},
			tabAtive : tabAtive,
			chooseType : chooseType,//选择分类
			cancleChoose : cancleChoose,//取消筛选
		}
	})
}

function cancleChoose(){
	chooseGoodsTypeOid = '';
	vu.isShow = false;
	$(".ag_backbg").removeClass("ag_backbg");
}

function tabAtive(e,type){
	var _this = vu;
	$(".tab-link").removeClass("tab_active");
	$(".tab-link").children("span").removeClass("tab_active");
	$(".tab-link").find("i").removeClass("tab_active");
	if(type == ''){
		vu.chooseGoodsTypeOid = '';
		_this.isShow = false;
		_this.GoodsList = [];
		$(e.currentTarget).addClass("tab_active");
		_this.sortNum = "";
		
		vu.pageIndex = 1;
		mescroll.resetUpScroll();
	}
	if(type == 'sale'){
		vu.chooseGoodsTypeOid = '';
		_this.isShow = false;
		_this.GoodsList = [];
		$(e.currentTarget).addClass("tab_active");
		_this.sortNum = 1;
		vu.pageIndex = 1;
		mescroll.resetUpScroll();
	}
	if(type == 'asc'){
		vu.chooseGoodsTypeOid = '';
		_this.isShow = false;
		_this.GoodsList = [];
		$(e.currentTarget).children("span").addClass("tab_active");
		$(e.currentTarget).children(".ag_tab_price").children(".icon_up").addClass("tab_active");
		_this.priceType = 'desc';
		_this.sortNum = 2;
		vu.pageIndex = 1;
		mescroll.resetUpScroll();

	}
	if(type == 'desc'){
		_this.isShow = false;
		_this.GoodsList = [];
		$(e.currentTarget).children("span").addClass("tab_active");
		$(e.currentTarget).children(".ag_tab_price").children(".icon_down").addClass("tab_active");
		_this.priceType = 'asc';
		_this.sortNum = 3;
		vu.pageIndex = 1;
		mescroll.resetUpScroll();
	}
	if(type == 'flit'){
		_this.sortNum = 4;
		$(e.currentTarget).addClass("tab_active");
		vu.isShow = !vu.isShow;
	}

}

function chooseType(index,oid,e){
	if(index == 1){
		$(".ag_backbg").removeClass("ag_backbg");
		$(".ag_ify_one").find("a").removeClass("ag_backbg");
	}
	if(index == 2){
		$(".ag_ify_two .ag_backbg").removeClass("ag_backbg");
		$(".ag_ify_three .ag_backbg").removeClass("ag_backbg");
		$(".ag_ify_two").find("a").removeClass("ag_backbg");
	}
	if(index == 3){
		$(".ag_ify_three .ag_backbg").removeClass("ag_backbg");
		$(".ag_ify_three").find("a").removeClass("ag_backbg");
	}
	$(e.currentTarget).addClass("ag_backbg");
	vu.chooseGoodsTypeOid = oid;
	if(index == 1 || index == 2){
		common.get(getGoodsTypeUrl,{ParentOID : vu.chooseGoodsTypeOid},"",function(r){
			if (r.ErrorCode == 0) {
				vu.IfyListTwo = r.Data.List;
				if(index == 1){
					vu.secondTypes = r.Data.List;
				}
				if(index == 2){
					vu.thirdTypes = r.Data.List;
				}
	        } else {
	            mui.toast(r.ErrorMsg);
	        }
		});
	}
}

function getGoodListByTypeOid(){
	if(!vu.chooseGoodsTypeOid){
		mui.toast("请选择类别");
		return;
	}
	vu.GoodsList = [];
	vu.isShow = false;
	vu.pageIndex = 1;
	mescroll.resetUpScroll();
}

function getGoodsType(){
	common.get(getGoodsTypeUrl, {},'json',function(r){
		if(r.ErrorCode==0){
			vu.firstTypes = r.Data.List;
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function getGoodsList(){
	var geturl = getGoodsListUrl;
	var data = {
		pageIndex : vu.pageIndex,
		pageSize : 10,
	};
	/*if(type){
		if(type == 'TJSP'){
			geturl = getRecommendGoodsUrl;
		}else{
			data.typeName = type;
			geturl = getGoodsListUrl;
		}
		
	}*/
	if(oid){
		data.GoodsTypeOID = oid;
		geturl = getGoodsListUrl;
	}
	if(goodName){
		data.GoodsName = goodName;
		geturl = searchGoodUrl;
	}
	if(vu.chooseGoodsTypeOid){
		data.GoodsTypeOID = vu.chooseGoodsTypeOid;
	}
	if(vu.sortNum == 1){
		data.SaleCountSort = '11';
	}
	if(vu.sortNum == 2){
		data.PriceSort = 'asc';
	}
	if(vu.sortNum == 3){
		data.PriceSort = '11';
	}
	if(vu.sortNum == 4){
		data.GoodsName = vu.name;
	}
	common.get(geturl, data,'json',function(r){
		if(r.ErrorCode==0){
			vu.TotalPageCount = r.Data.TotalPageCount;
			mescroll.endByPage(r.Data.TotalItemCount, vu.TotalPageCount);
			if(vu.pageIndex == 1){
				vu.GoodsList = r.Data.List;
			}else{
				vu.GoodsList = vu.GoodsList.concat(r.Data.List);
			}
    		vu.pageIndex++;
		}else{
			mui.toast(r.ErrorMsg);
			mescroll.endErr();
		}
	})
}
 
function initMescroll(){
	mescroll = new MeScroll("mescroll", {
		down:{
			use: false, //是否初始化下拉刷新; 默认true
			callback: function(mescroll) {
				vu.pageIndex = 1;
				getGoodsList();
				mescroll.resetUpScroll();
			}
		},
		up: {
			auto: true,
			isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
			clearEmptyId: "dataList", //1.下拉刷新时会自动先清空此列表,再加入数据; 2.无任何数据时会在此列表自动提示空
			toTop:{ //配置回到顶部按钮
				src : "phoneContent/img/top.png", //默认滚动到1000px显示,可配置offphoneContent/img/top.pngset修改
				//offset : 1000
			},
			htmlNodata: '<p class="upwarp-nodata">-- 没有更多数据了 --</p>', //无数据的布局
			callback: function (){//上拉回调
				getGoodsList();
			},
			page : {
				size : 10, 
			},
		}
	})
}


function bindEvent(){
	$("nav").on("tap", "a", function() { //点击触发  
		location.href = $(this).attr("href");
	})
}
